package StringOperation;

import monotonic_stack.Class84;

public class Class541 {

    public String reverseStr(String s, int k) {

        char[] chars = s.toCharArray();

        if (s.length()<=k){
           return    String.valueOf(reverse(chars,0,chars.length-1));
        }

        if (s.length()>k&&s.length()<2*k){
            return  String.valueOf(reverse(chars,0,k-1));
        }
        int count=0;

        for (int i = 2*k-1; i < chars.length; i+=2*k) {
            reverse(chars,i-2*k+1,i-k);
            count++;
        }
        int  leftstart=count*2*k;

        int end = chars.length-1;

        int left=end-leftstart+1;

        if (left<=k){
            return    String.valueOf(reverse(chars,leftstart,chars.length-1));
        }

        if (left>k&&left<2*k){
            return  String.valueOf(reverse(chars,leftstart,leftstart+k-1));
        }

        return  "";

    }

    public char[]  reverse(char[]  chars,int left,int right){
        while (left<right){
            char temp=chars[left];
            chars[left]=chars[right];
            chars[right]=temp;
            left++;
            right--;
        }
    return   chars;
    }


    public String reverseStr1(String s, int k) {
        char[] ch = s.toCharArray();
        for(int i = 0;i < ch.length;i += 2 * k){
            int start = i;
            // 判断尾数够不够k个来取决end指针的位置
            int end = Math.min(ch.length - 1,start + k - 1);
            while(start < end){

                char temp = ch[start];
                ch[start] = ch[end];
                ch[end] = temp;

                start++;
                end--;
            }
        }
        return new String(ch);
    }


    public static void main(String[] args) {

        Class541 class541 = new Class541();
        char[] cur={'a','b','c'};
//        char[] reverse = class541.reverse(cur, 0, 2);
//        for (char s : reverse) {
//            System.out.println(s);
//
//        }
         String  s="abcdefg";
        System.out.println(class541.reverseStr(s, 2));
    }

}
